[Pygments] Complete stubs for various modules#15610
[Pygments] Complete stubs for various modules#15610brianschubert wants to merge 6 commits intopython:mainfrom
Conversation
| def combine(*args: _Cats) -> str: ... | ||
| def allexcept(*args: _Cats) -> str: ... |
There was a problem hiding this comment.
note: these are always called with literal values in practice (example), so I think using a literal type here provides some useful validation (even if a little verbose in the stubs)
stubs/Pygments/pygments/util.pyi
Outdated
| def guess_decode_from_terminal(text: bytes, term: TextIO) -> tuple[str, str]: ... | ||
| def terminal_encoding(term: TextIO) -> str: ... |
There was a problem hiding this comment.
didn't feel worth using a more precise protocol since term is always sys.stdin / a proper file object in practice
There was a problem hiding this comment.
I'm on a quest to eliminate these pseudo-protocols (also see my comments on Mapping above). But TextIO is especially bad. A real protocol isn't really possible, until we get optional protocol members (python/typing#601). But I'd say even using Any here would be better, since these functions work with basically every object, unless encoding isn't a string field (unlikely).
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
srittau
left a comment
There was a problem hiding this comment.
Thanks, a few questions/optional remarks.
stubs/Pygments/pygments/__init__.pyi
Outdated
| def format(tokens: Iterator[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ... | ||
| @overload | ||
| def format(tokens, formatter: Formatter[_T], outfile: None = None) -> _T: ... | ||
| def format(tokens: Iterator[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: None = None) -> _T: ... |
There was a problem hiding this comment.
Shouldn't these be iterables?
| def format(tokens: Iterator[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ... | |
| @overload | |
| def format(tokens, formatter: Formatter[_T], outfile: None = None) -> _T: ... | |
| def format(tokens: Iterator[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: None = None) -> _T: ... | |
| def format(tokens: Iterable[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: SupportsWrite[_T]) -> None: ... | |
| @overload | |
| def format(tokens: Iterable[tuple[_TokenType, str]], formatter: Formatter[_T], outfile: None = None) -> _T: ... |
There was a problem hiding this comment.
arrgh, yes. Hasty tab completion strikes again
stubs/Pygments/pygments/util.pyi
Outdated
| def get_choice_opt( | ||
| options: Mapping[str, Any], optname: str, allowed: Container[_T], default: _T | None = None, normcase: bool = False | ||
| ) -> _T: ... | ||
| def get_bool_opt(options: Mapping[str, Any], optname: str, default: bool | None = None) -> bool: ... | ||
| def get_int_opt(options: Mapping[str, Any], optname: str, default: int | None = None) -> int: ... | ||
|
|
||
| # Return type and type of 'default' depend on the signature of the function whose **kwargs | ||
| # are being processed. | ||
| def get_list_opt(options: Mapping[str, Any], optname: str, default: list[Any] | tuple[Any, ...] | None = None) -> list[Any]: ... |
There was a problem hiding this comment.
Considering that call these just call get() on options, I would prefer a simple protocol here instead of the heady-handed Mapping pseudo-protocol. In fact I think such a protocol would be useful to have in _typeshed. (Although we can't really do the latter in this PR, since stdlib changes need a bit of time to propagate through type checkers.)
stubs/Pygments/pygments/util.pyi
Outdated
| def guess_decode_from_terminal(text: bytes, term: TextIO) -> tuple[str, str]: ... | ||
| def terminal_encoding(term: TextIO) -> str: ... |
There was a problem hiding this comment.
I'm on a quest to eliminate these pseudo-protocols (also see my comments on Mapping above). But TextIO is especially bad. A real protocol isn't really possible, until we get optional protocol members (python/typing#601). But I'd say even using Any here would be better, since these functions work with basically every object, unless encoding isn't a string field (unlikely).
Co-authored-by: Sebastian Rittau <srittau@rittau.biz>
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
No description provided.